import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BootStrap {
    private int port =8080;

  private   WebApps webApps = new WebApps();
    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public void start(int port) throws Exception {
        loadServer();
        this.port = port;
        ServerSocket serverSocket = new ServerSocket(port);
        while (true){
            Socket socket = serverSocket.accept();
             Request request = new Request(socket.getInputStream());
             Response response = new Response(socket.getOutputStream());
             if(request.getUrl()==null){

             }else {
                 HttpServlet httpServlet =  webApps.getServletMap().get(request.getUrl());
                 httpServlet.service(request,response);

             }
            response.outputHtml(request.getUrl());
            socket.close();

        }
    }

    private void loadServer() throws  Exception {
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("server.xml");
        SAXReader reader = new SAXReader();
        Document document = reader.read(resourceAsStream);
        Element rootElement = document.getRootElement();
        Element hostElement = (Element) rootElement.selectSingleNode("Host");
        String appBase = hostElement.attributeValue("appBase");
        //文件遍历
        File file = new File(appBase);
        loadWebApp(file);
    }

    /**
     * 加载webapp
     * @param file
     * @throws Exception
     */
    private void loadWebApp(File file) throws Exception {
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            if(files[i].isDirectory()){
                loadWebApp(files[i]);
            }else {
                if(files[i].getName().equals("web.xml")){
                    //进行servlet加载
                    SAXReader reader = new SAXReader();
                    Document document = reader.read(files[i]);
                    Element rootElement = document.getRootElement();
                    List<Element> list = rootElement.selectNodes("//servlet");
                    for (int j = 0; j < list.size(); j++) {
                        Element element = list.get(j);
                        Element servletNameNode = (Element) element.selectSingleNode("servlet-name");
                        String serlvetName = servletNameNode.getStringValue();
                        Element servletClassNameNode = (Element) element.selectSingleNode("servlet-class");
                        String serlvetClassName = servletClassNameNode.getStringValue();
                        Node servletMapping = element.selectSingleNode("/web-app/servelt-mapping[serlvet-Name = ']" + serlvetName + "']");
                        String stringValue = servletMapping.selectSingleNode("url-pattern").getStringValue();
                        webApps.getServletMap().put(stringValue, (HttpServlet) Class.forName(serlvetClassName).newInstance());

                    }

                }
            }
        }

    }



    public static void main(String[] args) {

    }
}
